<template>
	<view class="store-container">
		<view class="store-info">
			<view class="store-left">
				<view class="top">
					<view class="store-logo">
						<image :src="$imageUrl + storeInfo.logo" v-if="storeInfo.logo"></image>
					</view>
					<view class="store-name">
						<view class="name">
							{{ storeInfo.storeName }}
						</view>
						<view class="goods-count">
							{{ goodsCount }}款商品
						</view>
					</view>
				</view>
				<view class="bottom">
					{{ storeInfo.storeDesc }}
				</view>
			</view>
			<view class="store-right">
				<view class="subscribe">
					+订阅
				</view>
				<view class="subscribe-total">
					484+万人订阅
				</view>
			</view>
		</view>

		<view class="store-goods">
			<view class="controller-box">
				<scroll-view class="type-wrapper" scroll-x>
					<view class="type" :class="[currentIndex === -1 ? 'active' : '']" @click="changeType('', -1)">全部</view>
					<view class="type" :class="[currentIndex === index ? 'active' : '']" @click="changeType(item.id, index)" v-for="(item, index) in typeOptions" :key="item.id">{{ item.name }}</view>
				</scroll-view>
				<view class="condition-box">
					<view class="condition" :class="[current === index ? 'active' : '']"
						v-for="(item, index) in conditions" :key="index" @click="changeHandler(item, index)">
						{{ item }}
					</view>
					<view class="setting" @click="changeMode">
						<uni-icons type="gear" size="18"></uni-icons>
					</view>
				</view>
			</view>
			<view class="goods-list" :class="[mode ? 'flex' : '']" v-if="searchResult && searchResult.length > 0">
				<block v-for="goods in searchResult" :key="goods.goodsId">
					<navigator :url="`../goods_detail/goods_detail?goodsId=${goods.goodsId}`" :style="{ width: mode ? '50%' : '' }">
						<my-goods :goods="goods" :row="mode"></my-goods>
					</navigator>
				</block>
			</view>
			<view class="not-fount" v-else>
				<image src="../../static/cry.png"></image>
				暂无商品
			</view>
			<view :class="['filter-wrapper', show ? 'show' : 'none']">
				<view :class="['filter-mask', show ? 'show' : 'none']" @click.self="closeFilter"></view>
				<view :class="['filter-box', show ? 'show' : 'none']">
					<scroll-view scroll-y="true">
						<view class="terms-box">
							<view class="condition">价格区间</view>
							<view class="condition-value">
								<input type="text" placeholder="最低价" v-model="params.start" @blur="check">
								<text>—</text> <input type="text" placeholder="最高价" v-model="params.end" @blur="check">
							</view>
						</view>
						<view class="terms-box">
							<view class="condition">品牌</view>
							<view class="condition-value tagbox">
								<view class="tag" :class="params.brandId === item.id ? 'active': ''"
									v-for="item in brandOptions" :key="item.id" :text="item.name"
									@click="addBrandCondition(item)">
									{{ item.name }}
								</view>
							</view>
						</view>
						<view class="terms-box">
							<view class="condition">分类</view>
							<view class="condition-value tagbox">
								<view class="tag" :class="params.cateId === item.id ? 'active': ''"
									v-for="item in cateOptions" :key="item.id" :text="item.name"
									@click="addCateCondition(item)">
									{{ item.name }}
								</view>
							</view>
						</view>
					</scroll-view>
					<view class="btn-box">
						<view class="reset" @click="reset">
							重置
						</view>
						<view class="sure" @click="sure">
							确定
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current: 0,
				keywords: '',
				currentIndex: -1,
				historyList: [],
				recommendList: [],
				searchResult: [],
				brandOptions: [],
				cateOptions: [],
				typeOptions: [],
				conditions: ['综合', '销量', '价格', '新品', '筛选'],
				params: {
					keyword: '',
					cateId: '',
					// orderByPrice: 0,
					orderBySales: '',
					// fresh: '',
					page: 1,
					pageSize: 6,
				},
				storeInfo: {},
				storeId: '',
				total: 0,
				mode: true,
				show: false,
				loading: false,
				orderFlag: false,
				currentBrand: '',
				goodsCount: 0,
			}
		},
		computed: {
			brandItem() {
				console.log(this.params['brandId'] === undefined);
				return this.params['brandId'] === undefined
			}
		},
		onLoad(e) {
			const storeId = e.storeId
			this.storeId = storeId
			this.params.storeId = storeId
			this.getStoreInfo(storeId)
			this.searchGoodsList(this.params)
			this.conditionCate()
			this.conditionBrand()
			this.conditionType()
		},
		onPullDownRefresh() {
			this.params.page = 1
			this.searchResult = []
			this.loading = false
			this.searchGoodsList(this.params, () => {
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if(this.params.page * this.params.pageSize >= this.total) return uni.$showMsg('数据加载完毕 T_T')
			if(this.loading) return
			this.params.page++
			this.searchGoodsList(this.params)
		},
		methods: {
			clear() {
				this.keywords = ''
				this.searchResult = []
			},
			async getStoreInfo(storeId) {
				const { data: res } = await uni.$http.get(`/stores/${storeId}`)
				this.storeInfo = res.data
			},
			closeFilter() {
				this.show = false
				console.log('被点击了>_<');
			},
			changeMode() {
				this.mode = !this.mode
			},
			async changeHandler(item, index) {
				this.current = index
				if (item === '综合') {
					delete this.params.fresh
					delete this.params.orderByPrice
					console.log('params', this.params);
					// let params = { key: '', catId: '' }
					this.params.page = 1
					const {
						data: res
					} = await uni.$http.get(`/goods/store`, this.params)
					this.searchResult = res.data.records ? res.data.records : []
					this.total = res.data.total
				}
				if (item === '价格') {
					delete this.params.fresh
					console.log('params', this.params);
					if (this.orderFlag) {
						this.params.orderByPrice = 0
					} else {
						this.params.orderByPrice = 1
					}
					this.params.page = 1
					const {
						data: res
					} = await uni.$http.get(`/goods/store`, this.params)
					this.searchResult = res.data.records ? res.data.records : [],
					this.total = res.data.total
					this.orderFlag = !this.orderFlag
				}
				if (item === '新品') {
					delete this.params.orderByPrice
					console.log('params', this.params);
					this.params.fresh = 1
					this.params.page = 1
					const {
						data: res
					} = await uni.$http.get(`/goods/store`, this.params)
					console.log(res.data.records);
					this.searchResult = res.data.records ? res.data.records : [],
					this.total = res.data.total
				}
				if (item === '筛选') {
					this.show = !this.show
				}
			},
			async searchGoodsList(params, callback) {
				this.loading = true
				const {
					data: res
				} = await uni.$http.get(`/goods/store`, params)
				this.loading = false
				callback && callback()
				if (res.data.records) {
					this.searchResult = [...this.searchResult, ...res.data.records]
					this.total = res.data.total
					if(this.goodsCount === 0) {
						this.goodsCount = res.data.total
					}
				} else {
					this.searchResult = []
				}
			},
			async conditionBrand() {
				const {
					data: res
				} = await uni.$http.get(`/brands/store/${this.params.storeId}`)
				this.brandOptions = res.data
			},
			async conditionCate() {
				const {
					data: res
				} = await uni.$http.get(`/categories/store/${this.params.storeId}`)
				this.cateOptions = res.data
			},
			async conditionType() {
				const {
					data: res
				} = await uni.$http.get(`/types/${this.storeId}`)
				this.typeOptions = res.data
			},
			addBrandCondition(item) {
				console.log('brandId', item.id);
				if (this.params.brandId) {
					if (this.params.brandId === item.id) {
						delete this.params.brandId
					} else {
						this.params.brandId = item.id
					}
				} else {
					this.params.brandId = item.id
				}
				this.params = JSON.parse(JSON.stringify(this.params))
			},
			addCateCondition(item) {
				if (this.params.cateId) {
					if (this.params.cateId === item.id) {
						delete this.params.cateId
					} else {
						this.params.cateId = item.id
					}
				} else {
					this.params.cateId = item.id
				}
				this.params = JSON.parse(JSON.stringify(this.params))
			},
			async changeType(typeId, index) {
				this.currentIndex =  index
				this.params.typeId = typeId
				const {
					data: res
				} = await uni.$http.get(`/goods/store`, this.params)
				this.searchResult = res.data.records ? res.data.records : []
			},
			reset() {
				delete this.params.brandId
				delete this.params.cateId
				delete this.params.start
				delete this.params.end
				this.params = JSON.parse(JSON.stringify(this.params))
				console.log(this.params);
			},
			check() {
				if (this.params.start && this.params.end) {
					if (this.params.start > this.params.end) {
						let temp = this.params.end
						this.params.end = this.params.start
						this.params.start = temp
					}
				}
			},
			async sure() {
				this.params.page = 1
				const {
					data: res
				} = await uni.$http.get(`/goods/store`, this.params)
				this.searchResult = res.data.records ? res.data.records : []
			}
		},
	}
</script>

<style lang="scss" scoped>
	.store-container {
		width: 100%;
		.store-info {
			width: 100%;
			padding: 40rpx;
			display: flex;
			background-color: #000;
			color: #fff;
			justify-content: space-between;
			box-sizing: border-box;
		}

		.store-left,
		.store-right {
			margin: 40rpx 0;
		}
		
		.store-left {
			width: 70%;
		}
		
		.top {
			display: flex;
			align-items: center;
			margin-bottom: 30rpx;
		}
		
		.bottom {
			font-size: 12px;
			overflow: hidden; //多出的隐藏
			//text-overflow: ellipsis;//多出部分用...代替
			display: -webkit-box; //定义为盒子模型显示
			-webkit-line-clamp: 2; //用来限制在一个块元素显示的文本的行数
			-webkit-box-orient: vertical; //从上到下垂直排列子元素（设置伸缩盒子的子元素排列方式）
		}

		.store-logo {
			width: 60px;
			height: 60px;
			margin-right: 30rpx;
			border-radius: 50%;
			overflow: hidden;
			background-color: #fff;
			box-sizing: border-box;
			border: 4px solid #fff;
			box-shadow: 0 0 10px #bfa;
			image {
				width: 100%;
				height: 100%;
			}
		}

		.store-name {
			height: 60px;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
		}

		.name {
			font-size: 18px;
			font-weight: bold;
			margin-bottom: 10rpx;
		}

		.goods-count {
			font-size: 12px;
		}

		.store-right {
			display: flex;
			flex-direction: column;
			// justify-content: space-around;
			align-items: center;
		}

		.subscribe {
			width: 80%;
			height: 30px;
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;
			border-radius: 4px;
			justify-content: center;
			background-color: #52e3fc;
		}

		.subscribe-total {
			font-size: 12px;
		}
	}

	/deep/ scroll-view ::-webkit-scrollbar {
		display: none;
	}

	.store-goods {
		width: 100%;
		height: 100%;
		position: relative;
		top: -10px;
		border-radius: 10rpx;
		padding-top: 10rpx;
		background-color: #fff;
		.type-wrapper {
			width: 100%;
			height: 50px;
			line-height: 50px;
			white-space: nowrap;
			background-color: #fff;
			box-sizing: border-box;
			border-bottom: #f5f5f5 solid 1px;
		}

		.type {
			margin: 0 20rpx;
			font-size: 14px;
			display: inline-block;
			position: relative;
			&.active {
				font-weight: bold;
				&::after {
					position: absolute;
					content: "";
					width: 100%;
					height: 2px;
					left: 0;
					bottom: 0;
					background-color: #000;
				}
			}
		}
	}
	.controller-box {
		position: sticky;
		top: 44px;
		height: 88px;
		z-index: 777;
		background-color: #52e3fc;
	}

	.goods-list {
		background-color: #f5f5f5;
		// margin-top: 90px;
		&.flex {
			display: flex;
			flex-wrap: wrap;
		}
	}

	.condition-box {
		// top: 50px;
		width: 100%;
		height: 40px;
		display: flex;
		// position: sticky;
		box-sizing: border-box;
		// border-top: solid 1px #f5f5f5;
		border-bottom: solid 1px #f5f5f5;
		background-color: #fff;

		view {
			flex-grow: 1;
			color: #999;
			font-size: 12px;
			line-height: 40px;
			text-align: center;

			&.active {
				color: #000;
			}

			&.setting {
				width: 10px;
			}
		}
	}

	.filter-wrapper {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		right: 0;
		z-index: 888;

		&.show {
			transition: all .5s;
		}

		&.none {
			right: -100%;
			transition: all .5s;
		}
	}

	.filter-mask {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		right: 0;
		z-index: 888;
		background-color: rgba(0, 0, 0, .3);

		&.show {
			display: block;
			transition: all .3s;
		}

		&.none {
			display: none;
			transition: all .3s;
		}
	}

	.filter-box {
		width: 78%;
		height: 100%;
		padding: 20rpx;
		position: fixed;
		box-sizing: border-box;
		top: 0;
		right: 0;
		z-index: 999;
		background-color: #fff;

		&.show {
			right: 0%;
			transition: all .3s;
		}

		&.none {
			right: -100%;
			transition: all .3s;
		}

		.btn-box {
			width: 93%;
			display: flex;
			position: absolute;
			bottom: 20rpx;
			justify-content: space-between;

			view {
				height: 40px;
				line-height: 40px;
				text-align: center;
			}

			.reset {
				width: 35%;
				border: 1px solid #d9d9d9;
			}

			.sure {
				width: 60%;
				color: #fff;
				background-color: #52e3fc;
			}
		}
	}

	.terms-box {
		margin: 20rpx 0;
		width: 100%;

		.condition {
			font-size: 16px;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.condition-value {
			display: flex;
			align-items: center;

			input {
				height: 30px;
				line-height: 30px;
				text-align: center;
				padding: 10rpx 20rpx;
				font-size: 14px;
				box-sizing: border-box;
				background-color: #f0f0f0;
			}

			text {
				margin: 0 10rpx;
			}
		}

		.tagbox {
			display: flex;
			flex-wrap: wrap;

			.tag {
				font-size: 12px;
				height: 18px;
				line-height: 18px;
				margin: 8rpx 0;
				margin-right: 10px;
				font-weight: 200;
				padding: 4px 7px;
				color: #000;
				border-radius: 3px;
				background-color: #f5f5f5;

				&.active {
					color: #fff;
					background-color: #52e3fc;
				}
			}
		}
	}
	.not-fount {
		width: 100%;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding-top: 160rpx;
		flex-direction: column;
		justify-content: center;
		font-size: 20px;
		// color: #fff;
		color: #999;
		background-color: #f5f5f5;
		// text-shadow: 0 0 10px #52e3fc, 0 0 20px aqua;
		image {
			width: 120px;
			height: 120px;
			margin-bottom: 30rpx;
		}
		padding-bottom: 160rpx;
	}
</style>
